﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace VASMide
{
    class VASM : Language
    {
        public VASM()
        {
            this.name = "Viper Assembly";

            string keywords = "string dword bre bne jmp class end try catch call callinstance word byte newobj externlib push proc ret throw exception";
            foreach (string k in keywords.Split(' '))
            {
                this.keyWords.Add(k);
            }
            this.unindentKeywords.Add("end");
            this.indentKeywords.Add("proc");
            this.indentKeywords.Add("class");
            this.unindentKeywords.Add("ret");
        }
        public override void build(string source)
        {

            Viper.Assembler asm = new Viper.Assembler();
            asm.Assemble(source);
            asm.assembly.CreateGEX(Project.Path + @"\bin\app.gex");
            outPut = source;

        }
    }
    class Diamond : Language
    {
        public Diamond()
        {
            this.name = "Diamond";
            this.icon = VASMide.Properties.Resources.diamondlogo;
            this.Description = "Diamond is a high level object oriented language targeted on the VIPER platform.";
            string keywords = "string global this return function default break goto continue switch case throw class end out try if times for while int byte char void new var import package catch true false bool";
            foreach (string k in keywords.Split(' '))
            {
                this.keyWords.Add(k);
            }
            this.unindentKeywords.Add("end");
            this.FileExt = ".dmd";
            this.indentKeywords.Add("void");
            this.indentKeywords.Add("class");
            this.indentKeywords.Add("times");
            this.indentKeywords.Add("if");
            this.indentKeywords.Add("global");
            this.indentKeywords.Add("switch");
            this.indentKeywords.Add("while");
            this.indentKeywords.Add("function");
            this.indentKeywords.Add("for");
        }
        public override void build(string source)
        {

            Scanner s = new Scanner(new System.IO.StringReader(source));
            cobaltc.compiler c = new cobaltc.compiler(s.Tokens);
            
            c.compile();
            Form1.frm2.listBox1.Rows.Clear();
            if (c.Errors.Count > 0)
            {
                
                foreach (Error e in c.Errors)
                {
                    
                    Form1.frm2.listBox1.Rows.Add(e.Message,e.Line);
                }
                throw new Exception("Build Failed!");
            }
            source = c.ILCode;
            Viper.Assembler asm = new Viper.Assembler();
            foreach (string dll in Imports)
            {
                source += "externdll '" + System.IO.Path.GetFileName(dll) + "'\n";
            }
            
            foreach (string r in Project.References)
            {
                System.IO.File.Copy(r, Project.Path + @"\bin\" + System.IO.Path.GetFileName(r),true);
            }
            asm.Assemble(source);
            asm.assembly.CreateGEX(Project.Path + @"\bin\app.gex");
            asm.assembly.Classes.Clear();
            asm.assembly.DLLs.Clear();
            Viper.vClass.Calls.Clear();
            Viper.vClass.funcs.Clear();
            Viper.vClass.prevFuncs.Clear();
            outPut = source;




        }
    }


}
